package com.example.makerspace.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.makerspace.domain.AreaDO;
import com.example.makerspace.dto.Result;
import com.example.makerspace.dto.maker.AreaBaseDTO;
import com.example.makerspace.mapper.AreaMapper;
import com.example.makerspace.service.AreaService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

import static com.example.makerspace.enums.ResultCodeEnum.AREA_EXIST;

@Service
public class AreaServiceImpl implements AreaService {

    @Autowired
    private AreaMapper areaMapper;

    @Override
    public Result create(AreaBaseDTO areaBaseDTO) {
        QueryWrapper<AreaDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("area_name", areaBaseDTO.getAreaName());
        List<AreaDO> areaDOS = areaMapper.selectList(queryWrapper);
        if (areaDOS.size() > 0) {
            return Result.error(AREA_EXIST);
        }
        AreaDO areaDO = new AreaDO();
        areaDO.setAreaName(areaBaseDTO.getAreaName());
        areaMapper.insert(areaDO);
        return Result.success(true);
    }

    @Override
    public Result query() {
        QueryWrapper<AreaDO> queryWrapper = new QueryWrapper<>();
        List<AreaDO> areaDOS = areaMapper.selectList(queryWrapper);
        return Result.success(areaDOS);
    }

    @Override
    public Result delete(Integer id) {
        //TODO 判断是否有任务关联
        areaMapper.deleteById(id);
        return Result.success(true);
    }

    @Override
    public Result update(AreaBaseDTO areaBaseDTO) {
        QueryWrapper<AreaDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("area_name", areaBaseDTO.getAreaName());
        List<AreaDO> areaDOS = areaMapper.selectList(queryWrapper);
        if (areaDOS.size() > 0) {
            return Result.error(AREA_EXIST);
        }
        AreaDO areaDO = new AreaDO();
        areaDO.setId(areaDO.getId());
        areaDO.setModifyTime(new Date());
        areaDO.setAreaName(areaBaseDTO.getAreaName());
        areaMapper.updateById(areaDO);
        return Result.success(true);
    }


}
